package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.palm.hubo2o.R;
import com.palm.hubo2o.event.BetEvent;
import com.palm.hubo2o.model.Bet;
import com.palm.hubo2o.util.EnumCode;
import com.palm.palmutils.MathUtils;
import com.palm.palmutils.widget.EasyRecyclerView;

import de.greenrobot.event.EventBus;

/**
 * Created by dest1 on 2015/9/22.
 * 双色球普通
 */
public class SSQNormFragment extends MostLotteryFragment {

    private static final int MAX_NUMBER_RED = 33;
    private static final int MAX_NUMBER_BlUE = 16;

    private final Bet bet = new Bet(EnumCode.gameId.SSQ);


    @Override
    public void onCreateView2(View view, Bundle savedInstanceState) {
        super.onCreateView2(view, savedInstanceState);
        setupBalls("%02d", 1, MAX_NUMBER_RED, MAX_NUMBER_BlUE);
    }

    @Override
    protected void onSetMarkText(View parent, int groupIndex) {
        TextView title = (TextView) parent.findViewById(R.id.title_mark);
        TextView tip = (TextView) parent.findViewById(R.id.tip_mark);
        if (groupIndex == 0) {
            title.setText("红球");
            tip.setTextColor(red);
            tip.setText("至少选择6个红球");
        } else {
            title.setText("蓝球");
            tip.setTextColor(blue);
            tip.setText("至少选择1个蓝球");
        }
    }


    @Override
    protected void onClearChoices() {
        super.onClearChoices();
        bet.getReds().clear();
        bet.getBlues().clear();
    }

    @Override
    public boolean onRandomBalls() {
        onClearChoices();
        int[] random = MathUtils.randomIn(33, 6);
        for (int aRandom : random) {
            mGrid.setItemChecked(getAdapter().transToRawPosition(aRandom, 0), true);
        }
        mGrid.setItemChecked(getAdapter().transToRawPosition(MathUtils.random(MAX_NUMBER_BlUE), 1), true);
        return true;
    }


    @Override
    protected int onSetGroupColor(int groupIndex) {
        if (groupIndex == 0) {
            return red;
        } else {
            return blue;
        }
    }

    @Override
    protected boolean onBallChecked(EasyRecyclerView parent, boolean checked, int position, String ballText, int index) {
        if (checked && bet.getReds().size() + bet.getBlues().size() == 28) {//红蓝一共最多28
            Toast.makeText(getContext(), "红球、篮球总共最多28个", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (index == 0) {//红球区
            if (checked) {
                if (bet.getReds().size() == 20) {
                    Toast.makeText(getContext(), "最多选择20个红球", Toast.LENGTH_SHORT).show();
                    return false;
                }
                bet.getReds().add(ballText);
            } else bet.getReds().remove(ballText);
        } else {//蓝球区
            if (checked) {
                bet.getBlues().add(ballText);
            } else bet.getBlues().remove(ballText);
        }
        if (bet.getReds().size() >= 6 && bet.getBlues().size() >= 1) {
            bet.setWagerCount((int) (MathUtils.recursive(bet.getReds().size(), 6) / MathUtils.recursive(6)));//Cn6
            bet.setPlayType(bet.getWagerCount() == 1 ? EnumCode.PlayType.SSQ_DS : EnumCode.PlayType.SSQ_FS);
            EventBus.getDefault().post(new BetEvent(bet));
        } else {
            EventBus.getDefault().post(new BetEvent());
        }
        return true;
    }
}
